<?php
namespace app\common\model;
class Live extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    /* 获取播放信息 */
    public static function getLiveInfo ($time=60) {
      $liveInfo = [
        'push' => self::rebuildUrl('rtmp://'.config('aliyun_live_push_domain')."/".config('aliyun_live_appname')."/".config('aliyun_live_streamname'),1,$time),
        'pull' => [
          'default' => self::rebuildUrl('rtmp://'.config('aliyun_live_pull_domain')."/".config('aliyun_live_appname')."/".config('aliyun_live_streamname'),2,$time),
          'flv' => self::rebuildUrl('http://'.config('aliyun_live_pull_domain')."/".config('aliyun_live_appname')."/".config('aliyun_live_streamname').".flv",2,$time),
          'm3u8' => self::rebuildUrl('http://'.config('aliyun_live_pull_domain')."/".config('aliyun_live_appname')."/".config('aliyun_live_streamname').".m3u8",2,$time)
        ]
      ];
      return $liveInfo;
    }

    /* 重组信息+签名 */
    public static function rebuildUrl ($url,$type=1,$time=60) {
      $arr = parse_url($url);
      $key = $type==1?config('aliyun_live_push_key'):config('aliyun_live_pull_key');
      $string = (time()+$time*60)."-".uniqid()."-0-";
      $sign = md5($arr['path']."-".$string.$key);
      return "{$url}?auth_key={$string}{$sign}";
    }
}